package com.canbot.u05.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.canbot.u05.api.U05RobotManger;
import com.canbot.u05.receivers.VoiceResultEventReceiver;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.Utils;
import com.canbot.u05.view.FrameAnimation;
import com.canbot.u05.view.FrameAnimationFromSD;

import java.util.ArrayList;

public class BaseAcitivity extends BaseActivityNoTitle implements U05RobotManger.WakeUpReceiver {

        public static final int XI = 0;

        public static final int NU = 1;

        public static final int AI = 2;

        public static final int SI = 3;

        public static final int KONG = 4;

        private static final String TAG = "BaseAcitivity";

        Handler mHandler = new Handler() {
                public void handleMessage(android.os.Message msg) {
                        switch (msg.what) {
                                case 1:
                                        ViewGroup viewgroup = (ViewGroup) msg.obj;
                                        // switch (msg.what) {
                                        // AnimStop();
                                        // }
                                        AnimStop();
                                        // mIv.setBackgroundDrawable(null);
                                        // mFl.setVisibility(View.INVISIBLE);
                                        // micImg.setVisibility(View.VISIBLE);
                                        // viewgroup.setVisibility(View.VISIBLE);

                                        break;

                                case 2:
                    /*ArrayList<String> BitmapStrPath = ((MainApplication) getApplicationContext())
                    .getmAnimation_guide();
                    startGuidAnim(BitmapStrPath, 2000);*/
                                        break;
                        }

                }

                ;
        };

        public FrameAnimation micImg = null;

        public FrameAnimationFromSD micImgFromSD = null;

        public ImageView mShowIV = null;

        // protected ImageView micImg = null;
        private Context mContext;

        protected RelativeLayout mMicLayout;

        //    private AnimaReceiver mReceiver;
        byte[] mPlaySound = new byte[]{(byte) 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00};

        public FrameLayout mFl;

        public ImageView mIv;

        public ViewGroup mViewGroup;

        private VoiceResultEventReceiver mVoiceResultReceiver;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                mContext = this;
        }

        @Override
        protected void onResume() {
                super.onResume();
                hideBottomUIMenu();
                addVCA();
                addAnimaView();
                addVCAFromSd();
                // playResumeSound();

//        mReceiver = new AnimaReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("com.uurobot.xi");
//        filter.addAction("com.uurobot.nu");
//        filter.addAction("com.uurobot.ai");
//        filter.addAction("com.uurobot.si");
//        filter.addAction("com.uurobot.kong");
//        filter.addAction("com.uurobot.jing");
//        filter.addAction("com.uurobot.normal");
//        filter.addAction("com.uurobot.receive");
//        filter.addAction("com.uurobot.recognize");
//        filter.addAction("com.uurobot.start");
//        filter.addAction("com.uurobot.stop");
//        filter.addAction("com.uurobot.MyViewPager1");
//        filter.addAction("com.uurobot.KnowAboutUUActivity");
//        filter.addAction("com.uurobot.FunctionUUActivity");
//        filter.addAction("com.uurobot.SceneActivity");
//        filter.addAction("com.uurobot.AIActivity");
//        filter.addAction("com.uurobot.PersonalityActivity");
//        filter.addAction("com.uurobot.InteractionActivity");
//        filter.addAction("com.uurobot.IdentifyActivity");
//        filter.addAction("com.uurobot.PositionActivity");
//        filter.addAction("com.uurobot.IdentifyActivity");
//        filter.addAction("com.uurobot.IndustryActivity");
//        filter.addAction("com.uurobot.VoicecontrolActivity");
//        filter.addAction("com.uurobot.SensorActivity");
//        filter.addAction("com.uurobot.back");
//        filter.addAction("com.uurobot.show.photo");
//        registerReceiver(mReceiver, filter);
                mVoiceResultReceiver = new VoiceResultEventReceiver(mContext);
                mVoiceResultReceiver.registerBR();
//        U05RobotManger.newInstance().registerWakeUpReceiver(this, this);
                Logger.v(TAG, "onResume:");
        }

        public void addVCA() {
                // micImg = new ImageView(mContext);
                micImg = new FrameAnimation(mContext);
                // micImg.setVisibility(View.INVISIBLE);
                RelativeLayout.LayoutParams mm = new RelativeLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, 50);
                mm.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                mm.bottomMargin = 50;
                mm.addRule(RelativeLayout.CENTER_HORIZONTAL);
                mMicLayout.addView(micImg, mm);

        }

        public void addVCAFromSd() {
                // micImg = new ImageView(mContext);
                Logger.v(TAG, "micImgFromSD -- " + 2);
                micImgFromSD = new FrameAnimationFromSD(mContext);
                // micImg.setVisibility(View.INVISIBLE);
                RelativeLayout.LayoutParams mm = new RelativeLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                // mm.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                // mm.bottomMargin = 50;
                mm.addRule(RelativeLayout.CENTER_IN_PARENT);
                mMicLayout.addView(micImgFromSD, mm);
        }

        public void addAnimaView() {
                mFl = new FrameLayout(this);
                mIv = new ImageView(this);
                RelativeLayout.LayoutParams mm = new RelativeLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                // mIv.setBackgroundColor(Color.YELLOW);
                mFl.addView(mIv);
                mFl.setVisibility(View.INVISIBLE);
                mMicLayout.addView(mFl, mm);
        }

        protected void removeView() {
                if (mMicLayout != null) {
                        if (micImg != null) {
                                mMicLayout.removeView(micImg);
                                micImg = null;
                        }
                        if (mFl != null) {
                                mMicLayout.removeView(mFl);
                                mFl = null;
                        }
                        if (micImgFromSD != null) {
                                mMicLayout.removeView(micImgFromSD);
                                micImgFromSD = null;
                        }
                }
        }

        @Override
        protected void onPause() {
                super.onPause();

                removeView();
//        unregisterReceiver(mReceiver);
                mVoiceResultReceiver.unRegisterBR();
//        U05RobotManger.newInstance().unRegisterWakeUpReceiver(this);
                Logger.v(TAG, "onPause:");

        }

//        public void startAnimReceive() {
//                PhotoStop();
//                if (micImg == null)
//                        addVCA();
//                if (micImg != null) {
//                        micImg.stop();
//                        micImg.setBitmapResoursID(mResIdReceives);
//                        micImg.start();
//                }
//        }

        public void startGuidAnim(ArrayList<String> BitmapStrPath, int gap) {

                AnimStop();
                micImgFromSD = new FrameAnimationFromSD(mContext);
                micImgFromSD.setGapTime(gap);
                micImgFromSD.setRestart(false);
                ((FrameAnimationFromSD) micImgFromSD).setMapPath(BitmapStrPath);
                RelativeLayout.LayoutParams mm = new RelativeLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                mm.addRule(RelativeLayout.CENTER_IN_PARENT);
                mMicLayout.addView(micImgFromSD, mm);
                micImgFromSD.start();
                PhotoStop();
        }

        public void startGuidPhoto(String photoPath) {
                PhotoStop();
                AnimStop();
                Bitmap mBitmap = Utils.fromSD(ConstUtils.SD_Path_Image_XQ + photoPath);
                if (mBitmap != null) {
                        mShowIV = new ImageView(mContext);
                        mShowIV.setImageBitmap(mBitmap);
                        RelativeLayout.LayoutParams mm = new RelativeLayout.LayoutParams(
                                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
                        mm.addRule(RelativeLayout.CENTER_IN_PARENT);
                        mMicLayout.addView(mShowIV, mm);
                }

        }

        public void startAnim(ArrayList<String> BitmapStrPath, int delay) {
                PhotoStop();
                Logger.v(TAG, "micImgFromSD -- " + 1);
                if (micImgFromSD == null)
                        addVCAFromSd();
                if (micImgFromSD != null) {
                        Logger.v(TAG, "micImgFromSD -- " + BitmapStrPath);
                        micImgFromSD.stop();
                        ((FrameAnimationFromSD) micImgFromSD).setMapPath(BitmapStrPath);
                        micImgFromSD.start();
                        mHandler.sendEmptyMessageDelayed(1, delay);
                }
        }


        public void startAnim(ArrayList<String> BitmapStrPath) {
                PhotoStop();
                Logger.v(TAG, "micImgFromSD -- " + 1);
                if (micImgFromSD == null)
                        addVCAFromSd();
                if (micImgFromSD != null) {
                        Logger.v(TAG, "micImgFromSD -- " + BitmapStrPath);
                        micImgFromSD.stop();
                        ((FrameAnimationFromSD) micImgFromSD).setMapPath(BitmapStrPath);
                        micImgFromSD.start();
                }
        }


        public void AnimStop() {
                if (micImgFromSD != null) {
                        micImgFromSD.stop();
                        mMicLayout.removeView(micImgFromSD);
                        micImgFromSD = null;
                }
        }

        public void PhotoStop() {
                if (mShowIV != null) {
                        mMicLayout.removeView(mShowIV);
                        mShowIV = null;
                }
        }

//        public void startAnimRecog() {
//                if (micImg != null) {
//                        micImg.stop();
//                        micImg.setBitmapResoursID(mResIdRecos);
//                        micImg.start();
//                }
//        }
//
//        public void startAnimStart() {
//                if (micImg != null) {
//                        micImg.stop();
//                        micImg.setBitmapResoursID(mResIdStarts);
//                        micImg.start();
//                }
//        }

        public void startAnimStop() {
                if (mMicLayout != null && micImg != null) {
                        mMicLayout.removeView(micImg);
                        micImg = null;
                }
        /*
         * micImg.stop(); micImg.setBitmapResoursID(mResIdEnd); micImg.start();
         * SystemClock.sleep(100); micImg.stop();
         */

        }

        protected void playResumeSound() {
                // int[] action = { 0x06, 0x07, 0x08, 0x09, 0x0c, 0x0D, 0x26, 0x25,
                // 0x27,
                // 0x28 }; //里面部分动作要么太长， 要么 不会回中
                // playDirSome(ConstUtils.PATH_AI_QNQN_MYROBOT, getTips(), true);
        }

        protected String[] getTips() {
                return new String[]{"kjy_u05_001.wav"};
        }

        // protected void playDirSome(String dirPath, String fileName[], final
        // boolean reWake) {
        // if (mSound == null) {
        // mSound = new Sound(mContext);
        // }
        // mSound.playDirSome(dirPath, fileName, new PlayStateListener() {
        // @Override
        // public void playResutlState(boolean result) {
        // // playTipVoice(false,reWake);
        // if (result && micImg != null) {
        //
        // // startAnimRecog();
        // }
        // // startAnimrecognize();
        // // mHandler.sendEmptyMessageDelayed(1, 6000);
        // }
        // });
        // }

        protected void startotherActivity(Context context, Class<?> cls) {
                Intent intent = new Intent(context, cls);
                startActivity(intent);
        }


        /**
         * @param action
         * @param name
         * @param format
         * @param values len,time,begin,Flag
         */
        public void startEmotionAnima(String action, String name, String format,
                                      int... values) {
                Intent in = new Intent();
                in.setAction(action);
                in.putExtra("name", name);
                in.putExtra("format", format);
                in.putExtra("len", values[0]);
                in.putExtra("time", values[1]);
                in.putExtra("begin", values[2]);
                in.addFlags(values[3]);
                startActivity(in);
        }

//    /**
//     * 发送byte数据给3288
//     * 
//     * @param message
//     */
//    public void writeMes(int message) {
//        // byte[] data = { (byte) (message & 0xff), (byte) ((message >> 8) &
//        // 0xff) };
//        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND,
//                ((Integer) message).toString());
//    }

        @Override
        protected void onDestroy() {
//        writeMes((byte) 0x69);
                mHandler.removeMessages(1);
//        mHandler.removeMessages(2);
                super.onDestroy();
        }

//    public void playAnima(AnimationDrawable drawable, int Flag, int delay,
//            ViewGroup viewgroup) {
//        viewgroup.setVisibility(View.INVISIBLE);
//        startAnimStop();
//        mFl.setVisibility(View.VISIBLE);
//        mIv.setBackgroundDrawable(drawable);
//
//        drawable.start();
//        Message msg = mHandler.obtainMessage();
//        msg.what = Flag;
//        msg.obj = viewgroup;
//        mHandler.sendMessageDelayed(msg, delay);
//    }

        public void setViewGroup(ViewGroup viewgroup) {
                this.mViewGroup = viewgroup;
        }

        /**
         * 隐藏虚拟按键，并且全屏
         */
        protected void hideBottomUIMenu() {
                //隐藏虚拟按键，并且全屏
                //for new api versions.
                View decorView = getWindow().getDecorView();
                int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
                decorView.setSystemUiVisibility(uiOptions);
        }

        @Override
        public void onHandleWakeUpEvent() {
                Log.e(TAG, "onHandleWakeUpEvent");
                //startotherActivity(mContext, WakeUpActivity.class);
                startAnimStop();
                //finish();
        }

}
